import md5 from 'js-md5'
var publicLo = {
  // 本地存储
  sessionStorageSav: (key, vule) => {
    sessionStorage.setItem(key, vule)
  },
  // 取本地存储
  sessionStorageGet: (key) => {
    return sessionStorage.getItem(key) || ''
  },
  // 上传图片
  senImg: (domId, fn) => {
    // 获取文件
    var files = document.getElementById(domId).files[0]
    var size = document.getElementById(domId).files[0].size
    if(size<3){
      return  0
    }
    // 新建FileReader 对象
    var reader = new FileReader();
    // FileReader对象的readAsDataURL获取图片流
    reader.readAsDataURL(files);
    reader.onload = fn;
  },
  // 时间戳转换
  timeCheng: (time) => {
    var date
    if(time){
      date  = new Date(time)
    }else {
      date  = new Date()
    }
    var Y = date.getFullYear() + '-'
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' '
    var h = date.getHours()
    var m = date.getMinutes()
    var s = date.getSeconds()
    var week = date.getDay()
    week = publicLo.wek(week)
    if (m < 10) {
      m = '0' + m.toString()
    }
    if (M < 10) {
      M = '0' + M.toString()
    }
    if (D < 10) {
      D = '0' + D.toString()
    }
    if (h < 10) {
      h = '0' + h.toString()
    }
    h += ':'
    var tim = {
      ymd: Y + M + D,
      hms: h + m,
      times: Y + M + D + h + m,
      week: week,
      md: M + D,
      m: M,
      d: D,
      s: s,
      y: Y
    }
    return tim
  },
  // 日期转换时间戳
  dataChange: function (data) {
    var date, week, nexDate, today, time
    if (!data) {
      date = new Date()
    } else {
      date = new Date(data)
    }
    // 日期
    week = date.getDay()
    // 时间戳
    time = date.getTime()
    //
    today = this.timeCheng(time)
    var ymdTiem = today.times
    var y = today.y
    var m = today.m
    var d = today.d
    today = today.ymd
    nexDate = this.timeCheng(time + 86400000)
    nexDate = nexDate.ymd

    return {today: today, week: week, nexDate: nexDate, time: time, ymdTiem: ymdTiem, y: y, m: m, d: d}
  },
  // 最近一周时间
  weekDate: function (data, parm, nuber) {
    'use strict'
    if (!nuber) {
      nuber = 7
    }
    var date, week, nexDate, time
    date = new Date(data)
    time = date.getTime()
    var dat = []
    for (let i = 0; i < nuber; i++) {
      var timeDat = 86400000 * i
      if (parm == 'add') {
        nexDate = this.timeCheng(time + timeDat)
        dat.push(nexDate)
      } else {
        nexDate = this.timeCheng(time - timeDat)
        dat.unshift(nexDate)
      }
    }
    return dat
  },
  // 匹配星期几
  wek: (a) => {
    'use strict'
    a = parseInt(a)
    var res
    switch (a) {
      case 1:
        res = '星期一'
        break
      case 2:
        res = '星期二'
        break
      case 3:
        res = '星期三'
        break
      case 4:
        res = '星期四'
        break
      case 5:
        res = '星期五'
        break
      case 6:
        res = '星期六'
        break
      case 0:
        res = '星期日'
        break
    }
    return res
  },
  // 日历
  calendars: () => {
    'use strict'
  },
  // 固定时间 倒计时 （距离11 点 ）
  tmieOut (startTime, time, now) {
    'use strict'
    var jinzhi = 60 * 1000
    var time = parseInt(time) * jinzhi
    var now = now
    var timeOutsNow = now - startTime
    timeOutsNow = time - timeOutsNow
    var timeM = parseInt(timeOutsNow / jinzhi)
    var timeS = timeOutsNow % jinzhi
    timeS = parseInt(timeS / 1000)
    return {timeM: timeM, timeS: timeS}
  },
  // 对象排序
  sortData: (arr, key, up) => {
    // arr：对象，  key：排序key，up：false-从小到大，true-从大到小
    arr.sort(publicLo.sortDataParm(key, up))
    return arr
  },
  // 排序函数
  sortDataParm: (key, disc) => {
    return function (a, b) {
      return disc ? a[key] < b[key] : a[key] > b[key]
    }
  },
  // 组装 post parm
  parmToken: (parm) => {
    // 获取当前时间
    var date = new Date()
    var time = date.getTime()
    // 查看有无分页参数
    var page = null, pageSize = null
    if (parm.page) {
      page = parm.page
      pageSize = parm.pageSize
      // 去掉分页参数
      delete parm.page
      delete parm.pageSize
    }
    var dat = JSON.stringify(parm).split('').sort().join('').split('"').join('')
    var token = sessionStorage.getItem('token') || 'eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJjdXN0b21lciIsImlhdCI6MTUxMzA3NDEyMSwic3ViIjoiMCIsImV4cCI6MTUxNDU0NTM1MCwibmJmIjoxNTEzMDc0MTIxfQ.RonwXddXdXeZOp3-KhpwJxsTEOIFX_zigiE5BPGJo7w'
    var DATA = {
      token: token,
      timestamp: time,
      sign: md5(token + time + dat),
      bizId: 111,
      data: parm || '',
    }
    if(page){
      DATA.pager = {
          startNo: page ,
          endNo: pageSize
      }
    }
    return DATA
  },
  // 复制文本
  copyToClipboard: (txt) =>{
    if(window.clipboardData) {
      window.clipboardData.clearData();
      window.clipboardData.setData("Text",txt);
      alert("网址复制成功!快用Ctrl+V粘贴到QQ，MSN中发送给好友吧！")
    } else if(navigator.userAgent.indexOf("Opera") != -1)
    {
      window.location = txt;
      alert("网址复制成功!快用Ctrl+V粘贴到QQ，MSN中发送给好友吧！")
    } else if (window.netscape) {
      try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
      } catch (e) {
        alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");
      }
      var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
      if (!clip) return;
      var trans = Components.classes['@mozilla.org/widget/' +
      '' +
      ';1'].createInstance(Components.interfaces.nsITransferable);
      if (!trans) return;
      trans.addDataFlavor('text/unicode');
      var str = new Object();
      var len = new Object();
      var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
      var copytext = txt;
      str.data = copytext;
      trans.setTransferData("text/unicode",str,copytext.length*2);
      var clipid = Components.interfaces.nsIClipboard;
      if (!clip) return false;
      clip.setData(trans,null,clipid.kGlobalClipboard);
      alert("网址复制成功!快用Ctrl+V粘贴到QQ，MSN中发送给好友吧！")
    }
  },
  //滚动条
  scroll_To:(tar_y)=>{ //tar_y 即滑动条顶端 距离页面最上面的距离  
    //console.log('oooooooo')  
    var timer = setTimeout(function(){
      var current_y = document.body.scrollTop
      //console.log(current_y)  
    var  step = 40 //步长系数 即剩余的距离除以40 每1ms 移动一段距离  
      if (tar_y>current_y){ //tar_y > current_y 即向下滚动  
        var dist = Math.ceil((tar_y-current_y)/step)
        var next_y = current_y+dist
        if(next_y<tar_y){  //向上滚动和向下滚动判定的区别 是这里！！  
          window.scrollTo(0,next_y)
          publicLo.scroll_To(tar_y)
          console.log('向下')
        }
        else{
          window.scrollTo(0,tar_y)
          //clearTimeout(timer)  
        }
      }
      else{ //tar_y < current_y 即向上滚动  
        var dist = Math.floor((tar_y-current_y)/step)
        var next_y = current_y+dist
        if(next_y>tar_y){
          window.scrollTo(0,next_y)
          publicLo.scroll_To(tar_y)
          console.log('向上')
        }
        else{
          window.scrollTo(0,tar_y)
          //clearInterval(timer)  
        }
      }
    },1)
  }
}
const api = ''

export default {publicLo,api}
